package com.manor.dynamic.activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.manor.dynamic.R;
import com.manor.common.Constants;
import com.manor.common.adapter.ImChatFacePagerAdapter;
import com.manor.common.interfaces.OnFaceClickListener;
import com.manor.dynamic.bean.DynamicCommentBean;
import com.manor.dynamic.dialog.DynamicInputDialogFragment;
import com.manor.dynamic.views.DynamicCommentViewHolder;
import com.manor.dynamic.views.DynamicCommentVoiceViewHolder;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import java.util.List;

import androidx.viewpager.widget.ViewPager;


/**
 * Created by debug on 2019/7/24.
 *
 */

public abstract class AbsDynamicCommentActivity extends AbsDynamicActivity implements View.OnClickListener, OnFaceClickListener {

    protected DynamicCommentViewHolder mDynamicCommentViewHolder;
    protected DynamicInputDialogFragment mDynamicInputDialogFragmentiewHolder;
    private View mFaceView;//表情面板
    private int mFaceHeight;//表情面板高度
    protected DynamicCommentVoiceViewHolder mVideoCommentVoiceViewHolder;


    @Override
    protected void main() {
        super.main();

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_send) {
            if (mDynamicInputDialogFragmentiewHolder != null) {
                mDynamicInputDialogFragmentiewHolder.sendComment();
            }
        }
    }


    public void showVoiceViewHolder(final String dynamicId, final String dynamicUid, final DynamicCommentBean dynamicCommentBean) {
        AndPermission.with(this)
                .runtime()
                .permission(Permission.READ_EXTERNAL_STORAGE,Permission.WRITE_EXTERNAL_STORAGE,Permission.RECORD_AUDIO)//

                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        if (!lacksPermission(AbsDynamicCommentActivity.this, Permission.READ_EXTERNAL_STORAGE)
                                &&  !lacksPermission(AbsDynamicCommentActivity.this, Permission.WRITE_EXTERNAL_STORAGE)
                                &&!lacksPermission(AbsDynamicCommentActivity.this, Permission.RECORD_AUDIO)
                        ) {
                            if (mDynamicInputDialogFragmentiewHolder != null) {
                                mDynamicInputDialogFragmentiewHolder.hideSoftInput();
                            }
                            if (mVideoCommentVoiceViewHolder == null) {
                                mVideoCommentVoiceViewHolder = new DynamicCommentVoiceViewHolder(mContext, (ViewGroup) findViewById(R.id.rootView));
                            }
                            mVideoCommentVoiceViewHolder.setVideoId(dynamicId);
                            mVideoCommentVoiceViewHolder.setVideoUid(dynamicUid);
                            mVideoCommentVoiceViewHolder.setVideoCommentBean(dynamicCommentBean);
                            mVideoCommentVoiceViewHolder.addToParent();
                        }
                    }
                })
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        for (int i = 0; i < data.size(); i++) {

                            if (lacksPermission(AbsDynamicCommentActivity.this, Permission.WRITE_EXTERNAL_STORAGE)) {
                                Toast.makeText(AbsDynamicCommentActivity.this, getResources().getString(R.string.permissions_storage), Toast.LENGTH_SHORT).show();
                                if (AndPermission.hasAlwaysDeniedPermission(AbsDynamicCommentActivity.this, data)) {
                                    showSettingDialog(AbsDynamicCommentActivity.this, getResources().getString(R.string.permissions_storage), 10);
                                }
                                return;
                            }
                            if (lacksPermission(AbsDynamicCommentActivity.this, Permission.RECORD_AUDIO)) {
                                Toast.makeText(AbsDynamicCommentActivity.this, getResources().getString(R.string.permissions_audio), Toast.LENGTH_SHORT).show();
                                if (AndPermission.hasAlwaysDeniedPermission(AbsDynamicCommentActivity.this, data)) {
                                    showSettingDialog(AbsDynamicCommentActivity.this, getResources().getString(R.string.permissions_audio), 10);
                                }
                                return;
                            }
                        }
                    }
                })
                .start();

    }

    /**
     * 打开评论输入框
     */
    public void openCommentInputWindow(boolean openFace, String dynamicId, String dynamicUid, DynamicCommentBean bean,String msg) {
        if (mFaceView == null) {
            mFaceView = initFaceView();
        }
        DynamicInputDialogFragment fragment = new DynamicInputDialogFragment();
        fragment.setDynamicInfo(dynamicId, dynamicUid);
        Bundle bundle = new Bundle();
        bundle.putBoolean(Constants.VIDEO_FACE_OPEN, openFace);
        bundle.putInt(Constants.VIDEO_FACE_HEIGHT, mFaceHeight);
        bundle.putParcelable(Constants.VIDEO_COMMENT_BEAN, bean);
        bundle.putString(Constants.VIDEO_COMMENT_MSG, msg);
        fragment.setArguments(bundle);
        mDynamicInputDialogFragmentiewHolder = fragment;
        fragment.show(getSupportFragmentManager(), "VideoInputDialogFragment");
    }


    public View getFaceView() {
        if (mFaceView == null) {
            mFaceView = initFaceView();
        }
        return mFaceView;
    }

    /**
     * 初始化表情控件
     */
    private View initFaceView() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.view_chat_face, null);
        v.measure(0, 0);
        mFaceHeight = v.getMeasuredHeight();
        v.findViewById(R.id.btn_send).setOnClickListener(this);
        final RadioGroup radioGroup =  v.findViewById(R.id.radio_group);
        ViewPager viewPager =  v.findViewById(R.id.viewPager);
        viewPager.setOffscreenPageLimit(10);
        ImChatFacePagerAdapter adapter = new ImChatFacePagerAdapter(mContext, this);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                ((RadioButton) radioGroup.getChildAt(position)).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        for (int i = 0, pageCount = adapter.getCount(); i < pageCount; i++) {
            RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.view_chat_indicator, radioGroup, false);
            radioButton.setId(i + 10000);
            if (i == 0) {
                radioButton.setChecked(true);
            }
            radioGroup.addView(radioButton);
        }
        return v;
    }


    /**
     * 刷新评论
     */
    public void refreshComment() {
        if (mDynamicCommentViewHolder != null) {
            mDynamicCommentViewHolder.refreshComment();
        }
    }



    @Override
    protected void onPause() {
        super.onPause();
        if (mPlayView != null) {
            mPlayView.pausePlay();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mPlayView != null) {
            mPlayView.resumePlay();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPlayView != null) {
            mPlayView.destroy();
        }
        mPlayView = null;
    }

    @Override
    public void onFaceClick(String str, int faceImageRes) {
        if (mDynamicInputDialogFragmentiewHolder != null) {
            mDynamicInputDialogFragmentiewHolder.onFaceClick(str, faceImageRes);
        }
    }

    @Override
    public void onFaceDeleteClick() {
        if (mDynamicInputDialogFragmentiewHolder != null) {
            mDynamicInputDialogFragmentiewHolder.onFaceDeleteClick();
        }
    }

    public void release() {
        if (mDynamicCommentViewHolder != null) {
            mDynamicCommentViewHolder.release();
        }

        mDynamicCommentViewHolder = null;
        mDynamicInputDialogFragmentiewHolder = null;

    }

    public void releaseVideoInputDialog() {
        mDynamicInputDialogFragmentiewHolder = null;
    }

}
